/*
 *  atBudget Personal Finance Management application
 *
 *  Copyright 2011, Ilia Frenkel <frenkel.ilia@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
	Main application controller is responsible for main Viewport.
 */
Ext.define('ATB.controller.Main', {
    extend: 'Ext.app.Controller',
	views: [
		'main.North',
		'main.South',
		'main.Center'
	],
	stores: [
		'Transactions'
	],

	/*
		Setup event handlers.
	*/
    init: function() {
		this.control({
			'mainnorth menuitem[action=manageaccounts]': {
				click: this.manageAccounts
			},
			'mainnorth menuitem[action=managecurrencies]': {
				click: this.manageCurrencies
			},
			'mainnorth menuitem[action=managecategories]': {
				click: this.manageCategories
			},
			'mainnorth menuitem[action=managepayees]': {
				click: this.managePayees
			},
			'mainnorth button[action=preferences]': {
				click: this.notImplementedYet
			},
			'mainnorth button[action=help]': {
				click: this.notImplementedYet
			},
			'maincenter accountsimplelist': {
				selectionchange: this.changeSelectedAccount
			}
		});//this.control
	},//init

	/*
		Open manage accounts dialogue as a result of selecting
		Manage->Accounts from the top menu.
	*/
	manageAccounts: function() {
		var view = Ext.widget('accountmanage');
	},//manageAccounts

	/*
		Open manage currencies dialogue as a result of selecting
		Manage->Currencies from the top menu.
	*/
	manageCurrencies: function() {
		var view = Ext.widget('currencymanage');
	},//manageCurrencies

	/*
		Open manage payees dialogue as a result of selecting
		Manage->Payees from the top menu.
	*/
	managePayees: function() {
		var view = Ext.widget('payeemanage');
	},//managePayees

	/*
		Open manage categories dialogue as a result of selecting
		Manage->Categories from the top menu.
	*/
	manageCategories: function() {
		var view = Ext.widget('categorymanage');
	},//manageCategories

	changeSelectedAccount: function(sel, recs, opts) {
		//load transactions store with transactions for selected account
		if (recs.length > 0) {
			var acc = recs[0];
			var store = this.getTransactionsStore();
			store.selectedAccount = acc;
			store.load({
				filters: [{
					property: 'accountid',
					value: acc.data.id
				}]
			});
		}

		//fire event to others saying that selected account changed
		this.application.fireEvent('accountchanged', recs);
	},//changeSelectedAccount

	/*
		Show friendly "Not Implemented Yet" message when user selects
		a feature that is not implemented yet. Better than doing nothing.
	*/
	notImplementedYet: function() {
		Ext.Msg.show({
			title: 'Not implemented yet.',
			msg: 'Unfortunately this feature is not implemented yet.',
			buttons: Ext.Msg.OK,
			icon: 'atb-icon-exclamation'
		});
	}//notImplementedYet
});
